const App = {
    data() {
        return {
            siteSetting: {
                siteTitle: '',
                siteSubTitle: '',
                siteContent: '',
                siteKeyword: ''
            },
            managerUsers: {
                search: '',
                searchCondition: 1,
                itemsCount: 0,
                tableData: []
            },
            managerCreditCard: {
                dataRange: ['', ''],
                bankId: '',
                isPay: '',
                searchCon: 1,
                search: '',
                searchCondition: '',
                itemsCount: 0,
                tableData: []
            },
            managerBank: {
                search: '',
                updateBankId: '',
                updateBankName: '',
                addBankName: ''
            },
            BankMsgAll: []
        };
    },
    methods: {
        loadBankAll: async function () {
            await axios.get("/api/Bank/BankMsgServlet").then((req) => {
                const data = req.data;
                if (data.code > 0) {
                    location.reload();
                    return;
                }
                this.BankMsgAll = data.message;
            });
        },
        loadManagerCreditCard: async function (pageIndex) {
            const managerCreditCard = this.managerCreditCard;
            if (managerCreditCard.dataRange === null) {
                this.managerCreditCard.dataRange = ['', ''];
            }
            const minTime = managerCreditCard.dataRange[0];
            const maxTime = managerCreditCard.dataRange[1];
            const bankId = managerCreditCard.bankId;
            const cardState = managerCreditCard.isPay;
            const searchCon = managerCreditCard.searchCon;
            const search = managerCreditCard.search;
            await axios.post("/api/CreditCard/isAdmin/getAdminCreditCardAllServlet", {
                pageIndex: pageIndex,
                maxTime: maxTime,
                minTime: minTime,
                bankId: bankId,
                cardState: cardState,
                searchCon: searchCon,
                search: search
            }).then((req) => {
                const data = req.data;
                if (data.code > 0) {
                    this.tipsMessage(data.code, data.error);
                    setTimeout(() => {
                        location.reload()
                    }, 500);
                    return;
                }
                const mess = data.message;
                this.managerCreditCard.itemsCount = mess.itemsCount;
                this.managerCreditCard.tableData = mess.data;
            });
        },
        tabManagerCreditCardPage: function (val) {
            this.loadManagerCreditCard(val);
        },
        editBankMsg: function (rowData) {
            this.managerBank.updateBankId = rowData.bankId;
            this.managerBank.updateBankName = rowData.bankName;
        },
        addBankMsg: function () {
            const managerBank = this.managerBank;
            const bankName = managerBank.addBankName;
            axios.post("/api/Bank/isAdmin/addAdminBankServlet", {
                bankName: bankName
            }).then((req) => {
                const data = req.data;
                if (data.code > 0) {
                    this.tipsMessage(data.code, data.error);
                    return;
                }
                this.tipsMessage(data.code, data.message);
                this.loadBankAll();
            });
        },
        clearUpdateBankMsg: function () {
            this.managerBank.updateBankName = '';
            this.managerBank.updateBankId = '';
        },
        updateBankMsg: function () {
            const managerBank = this.managerBank;
            const bankId = managerBank.updateBankId;
            const bankName = managerBank.updateBankName;
            if (isEmpty(bankId, bankName)) {
                this.tipsMessage(1, "请选中需要修改的内容禁止留空");
                return;
            }
            axios.post("/api/Bank/isAdmin/updateAdminBankServlet", {
                bankId: bankId,
                bankName: bankName
            }).then((req) => {
                const data = req.data;
                if (data.code > 0) {
                    this.tipsMessage(data.code, data.error);
                    return;
                }
                this.tipsMessage(data.code, data.message);
                this.clearUpdateBankMsg();
                this.loadBankMsg();
                return;
            });
        },
        deleteBankMsg: function (bankId) {
            if (isEmpty(bankId)) {
                this.tipsMessage(2, "非法传参");
                return;
            }
            axios.post("/api/Bank/isAdmin/deleteAdminBankServlet", {
                bankId: bankId
            }).then((req) => {
                const data = req.data;
                if (data.code > 0) {
                    this.tipsMessage(data.code, data.error);
                    return;
                }
                this.tipsMessage(data.code, data.message);
                this.loadBankMsg();
            })
        },
        loadUserMsg: async function (pageIndex) {
            const managerUsers = this.managerUsers;
            const searchCon = managerUsers.searchCondition;
            const search = managerUsers.search;
            await axios.post("/api/User/isAdmin/getUsersAdminServlet", {
                pageIndex: pageIndex,
                searchCon: searchCon,
                search: search
            }).then((req) => {
                const data = req.data;
                if (data.code > 0) {
                    location.reload();
                    return;
                }
                const mess = data.message;
                this.managerUsers.tableData = mess.data;
                this.managerUsers.itemsCount = mess.itemsCount;
            })
        },
        tabUserMsgPage: function (val) {
            this.loadUserMsg(val);
        },
        deleteUser: function (userId) {
            if (isEmpty(userId)) {
                this.tipsMessage(2, "非法传参");
                return;
            }
            axios.post("/api/User/isAdmin/deleteUserAdminServlet", {
                userId: userId
            }).then((req) => {
                const data = req.data;
                if (data.code > 0) {
                    this.tipsMessage(data.code, data.error);
                    return;
                }
                this.tipsMessage(data.code, data.message);
                this.loadUserMsg(1);
            });
        },
        loadSiteAll: async function () {
            await axios.get("/api/Site/getSiteServlet").then((req) => {
                const data = req.data.message;
                this.siteSetting.siteTitle = data.siteTitle;
                this.siteSetting.siteSubTitle = data.siteSubTitle;
                this.siteSetting.siteContent = data.siteContent;
                this.siteSetting.siteKeyword = data.siteKeyword;
            });
        },
        updateSite: function () {
            const siteSetting = this.siteSetting;
            const title = siteSetting.siteTitle;
            const subTitle = siteSetting.siteSubTitle;
            const content = siteSetting.siteContent;
            const keyword = siteSetting.siteKeyword;
            if (isEmpty(title, subTitle, content, keyword)) {
                this.tipsMessage(2, "请不要留空");
                return;
            }
            axios.post("/api/Site/isAdmin/updateSiteAdminServlet", {
                title: title,
                subTitle: subTitle,
                content: content,
                keyword: keyword
            }).then((req) => {
                const data = req.data;
                if (data.code > 0) {
                    this.tipsMessage(data.code, data.error);
                    return;
                }
                this.tipsMessage(data.code, data.message);
                this.loadSiteAll();
            })
        },
        tipsMessage: function (code, message) {
            let type = '';
            if (code === 0) {
                type = 'success'
            } else if (code === 1) {
                type = 'info'
            } else if (code === 2) {
                type = 'warning'
            } else if (code === 3) {
                type = 'error'
            }
            this.$message({
                type: type,
                message: message
            });
        }
    },
    mounted() {
        const search = location.search.replace(/\?index=/, "").trim();
        if (search === "managerCreditCard") {
            this.loadBankAll();
            this.loadManagerCreditCard(1);
        } else if (search === "manageBank") {
            this.loadBankAll();
        } else if (search === "managerUsers") {
            this.loadUserMsg(1);
        } else {
            this.loadSiteAll();
        }
    }
};
const app = Vue.createApp(App);
app.use(ElementPlus);
app.mount("#app");
